package lambda;

import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class FilterTest {

    public static void main(String[] args) {
        List<String> nameList = Lists.newArrayList();
        nameList.add("Tony");
        nameList.add("Alice");
        nameList.add("Jason");
        nameList.add("Jack");
        nameList.add("Jack");

        //验证filter返回true，代表添加到集合中
        nameList = nameList.stream().filter(p->p.startsWith("J")).collect(Collectors.toList());
        System.out.println("nameList Size:" + nameList.size());

        //验证去重
        Map<String,String> nameMap = nameList.stream().filter(p->p.startsWith("J")).collect(Collectors.toMap(p->p, p->p,(o,n)-> o));
        System.out.println("nameMap Size:" + nameMap.size());

        Set<String> nameSet = nameList.stream().filter(p->p.startsWith("J")).collect(Collectors.toSet());
        System.out.println("nameSet Size:" + nameSet.size());
        //验证count方法
        System.out.println("Like Jas count:" + nameList.stream().filter(p -> p.startsWith("Jas")).count());

        List<Person> personList = Lists.newArrayList();
        personList.add(new Person("Jack", 18));
        personList.add(new Person("Jack", 19));
        personList.add(new Person("Jason", 17));
        Map<String, Person> personMapMap = personList.stream()
                .filter(p -> p.getName().startsWith("J"))
                .collect(Collectors.toMap(p -> p.getName(), p -> p, (o, n) -> o));
        System.out.println("personMapMap Size:" + personMapMap.size());
        System.out.println("Jack age:" + personMapMap.get("Jack").getAge());

    }

    @Data
    @AllArgsConstructor
    public static class Person{
        private String name;
        private Integer age;
    }
}
